package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lt.bcra.mapper.BcraFamilyHistoryMapper;
import com.lt.bcra.entity.BcraFamilyHistory;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.BcraFamilyHistoryService;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 个人及家族病史表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
public class BcraFamilyHistoryServiceImpl extends ServiceImpl<BcraFamilyHistoryMapper, BcraFamilyHistory> implements BcraFamilyHistoryService {

    @Autowired
    private BcraFamilyHistoryMapper bcraFamilyHistoryMapper;

    @Override
    public ResultData saveModule(List<BcraFamilyHistory> bcraFamilyHistoryList) {

        int count = 0;

        for (BcraFamilyHistory bcraFamilyHistory : bcraFamilyHistoryList) {
            bcraFamilyHistory.setModuleId(5);
            bcraFamilyHistory.setModuleTitle("个人及家族病史表");

            //todo 文件上传路径与id

            int ok = bcraFamilyHistoryMapper.insert(bcraFamilyHistory);
            if (ok != 1) {
                throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
            }
            count++;
        }
        return ResultData.getResult(count);
    }

    @Override
    public ResultData updateModule(List<BcraFamilyHistory> bcraFamilyHistoryList) {
        String questionnaireCode = bcraFamilyHistoryList.get(0).getQuestionnaireCode();
        QueryWrapper<BcraFamilyHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);

        int count = 0;

        for (BcraFamilyHistory bcraFamilyHistory : bcraFamilyHistoryList) {

            //todo 文件上传路径与id

            int ok = bcraFamilyHistoryMapper.update(bcraFamilyHistory, queryWrapper);
            if (ok != 1) {
                throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
            }
            count++;
        }
        return ResultData.getResult(count);
    }

    @Override
    public ResultData removeByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraFamilyHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        Integer ok = bcraFamilyHistoryMapper.delete(queryWrapper);
        System.out.println("ok==========++============>" + ok);
        if (ok < 1) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraFamilyHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        List<BcraFamilyHistory> bcraFamilyHistories = bcraFamilyHistoryMapper.selectList(queryWrapper);
        if (bcraFamilyHistories == null) {
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraFamilyHistories);
    }
}
